#!/usr/bin/env python

# pylazy: report on how do you spend your time with
# Copyright (C) 2009 Jordi Carrillo Bosch
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import sys 

from distutils.core import Command
from glob import glob
from os.path import splitext, basename, join as pjoin, walk
from subprocess import Popen,PIPE
import os,shutil


long_description = '''
Introduction
============
pylazy is an small command line app for Linux that reports how do 
you spend your time with in your computer. 

'''

__version__='0.2'

try:
    from distutils.core import setup 
except:
    print "You need to install distutils python module"
    sys.exit()



class Clean(Command):
    user_options = []
    
    def initialize_options(self):
        self.__toRemove = []
        for root,path,files in os.walk("."):
            for file in files:
                if file.endswith(('~','pyc','#')):
                    self.__toRemove.append(os.path.join(root,file))
    
    def finalize_options(self):
        pass
    
    def run(self):
        curdir = os.getcwd()
        distdir = pjoin(curdir,"dist")
        if os.path.exists(distdir):
            print "removing distribution directory"
            shutil.rmtree(distdir)
        
        manifest = pjoin(curdir,'MANIFEST')
        if os.path.exists(manifest):
            print "removing MANIFEST file"
            os.unlink(manifest)

        for file in self.__toRemove:
            try:
                print "removing "+file
                os.unlink(file)
            except:
                pass



class Release(Command):
    user_options = [('rtag',None,"definitive version, tag and release")]

    def initialize_options(self):
        self.rtag = False
    
    def finalize_options(self):
        pass

    def run(self):
        # check everything is ok
        if self.rtag:
            # push release to svn 
            # and tag it with release version
            httpcurrdir = 'https://pylazybreak.googlecode.com/svn/trunk/'
            httpdestdir = 'https://pylazybreak.googlecode.com/svn/tags/pylazybreak-'+__version__
            releasecomment = "PyLazy release version "+__version__
            svncommand = ['svn','copy',httpcurrdir,httpdestdir,'-m',releasecomment]
            svnproc = Popen(svncommand,stdout = PIPE)
            print "tagging project into googlecode svn"
            out,err = svnproc.communicate()
            print out
        
        self.run_command("sdist")
        if not os.path.isdir('dist'):
            print "I just run sdist and no dist??"
            sys.exit()




setup(name="pylazy",
      version=__version__,
      description="How do you spend your time?",
      long_description = long_description,
      author="Jordi Carrillo",
      author_email = "jordilin@gmail.com",
      url = "http://code.google.com/p/pylazybreak/",
      license = "GNU GPL v3",
      scripts = ["pylazy.py"],
      cmdclass = {"release":Release,"clean":Clean},
      classifiers=[
          'Development Status :: 5 - Production/Stable',
          'Environment :: Console',
          'Intended Audience :: Education',
          'Intended Audience :: End Users/Desktop',
          'License :: OSI Approved :: GNU General Public License (GPL)',
          'Operating System :: POSIX',
          'Programming Language :: Python',
          'Topic :: System :: Monitoring'
          ],
      )
